<?php

class M_public_function extends CI_Model{
	
	function M_public_function(){
		parent::__construct();
	}
	
	function clear_cache(){
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
		$this->output->set_header("Pragma: no-cache");
	}
	
	function kategori_list(){
		$sql = "SELECT m_kategori.* FROM m_kategori";
		$rs = $this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return NULL;
		}
	}
	function count_product($kategori){
		$kuery = "SELECT COUNT(*) as produk_count FROM m_produk";
		if($kategori !== "" && $kategori !== '0'){
			$kuery .= " WHERE produk_kategori = '" . $kategori . "'";
		}
		$query = $this->db->query($kuery);
		$row = $query->row();
		return $row->produk_count;
	}
	function all_product_list($kategori, $sort, $start, $end){
		$sql = "SELECT m_produk.* FROM m_produk";
		if($kategori !== "" && $kategori !== '0'){
			$sql .= " where produk_kategori = '" . $kategori . "'";
		}
		$sql .= " ORDER BY " . $sort . " LIMIT " . $start . "," . $end;
		$rs = $this->db->query($sql);
	
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return NULL;
		}
	}
	function product_searched($key){
		$sql = "SELECT m_produk.* FROM m_produk 
			LEFT JOIN m_kategori ON produk_kategori = kategori_id
			WHERE LOWER(produk_nama) LIKE '%" . strtolower($key) . "%' OR LOWER(produk_deskripsi) LIKE '%" . strtolower($key) . "%' 
			OR LOWER(produk_harga) LIKE '%" . strtolower($key) . "%' OR LOWER(kategori_nama) LIKE '%" . strtolower($key) . "%' OR LOWER(kategori_keterangan) LIKE '%" . strtolower($key) . "%'
		";
		$rs = $this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return NULL;
		}
	}
	
	function new_product_list(){
		/* $sql = "SELECT m_produk.*, TIMESTAMPDIFF(DAY,DATE_FORMAT(DATE(m_produk.produk_created_date),'%Y-%m-%d'),CURDATE()) AS days
				FROM m_produk HAVING days <= 5"; */
		$sql = "SELECT produk_id, produk_kode, produk_nama, SUBSTRING_INDEX(produk_deskripsi,' ',10) AS produk_deskripsi, produk_harga,
				produk_sampul, produk_file, produk_aktif, produk_kategori, produk_created_by, produk_created_date
			FROM m_produk order by produk_created_date DESC limit 0,5";
		/*$sql = "SELECT produk_id, produk_kode, produk_nama, LEFT(produk_deskripsi,50) AS produk_deskripsi, produk_harga,
				produk_sampul, produk_file, produk_aktif, produk_kategori, produk_created_by, produk_created_date
			FROM m_produk order by produk_created_date DESC limit 0,10";*/
		$rs = $this->db->query($sql);
		
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return NULL;
		}
	}
	
	function old_product_list(){
		/* $sql = "SELECT m_produk.*, TIMESTAMPDIFF(DAY,DATE_FORMAT(DATE(m_produk.produk_created_date),'%Y-%m-%d'),CURDATE()) AS days
				FROM m_produk HAVING days > 5"; */
		/*$sql = "select produk_id, produk_kode, produk_nama, SUBSTRING_INDEX(produk_deskripsi,' ',10) AS produk_deskripsi, produk_harga,
				produk_sampul, produk_file, produk_aktif, produk_kategori, produk_created_by, produk_created_date
			from m_produk where produk_id in (
				select history_produk
				from t_history
				group by history_produk
				order by count(*) desc
			)
			limit 0,10";*/
		$sql = "select produk_id, produk_kode, produk_nama, SUBSTRING_INDEX(produk_deskripsi,' ',10) AS produk_deskripsi, produk_harga,
				produk_sampul, produk_file, produk_aktif, produk_kategori, produk_created_by, produk_created_date
			from m_produk order by produk_created_date DESC limit 5,100";
		/*$sql = "select produk_id, produk_kode, produk_nama, LEFT(produk_deskripsi,50) AS produk_deskripsi, produk_harga,
				produk_sampul, produk_file, produk_aktif, produk_kategori, produk_created_by, produk_created_date
			from m_produk where produk_id in (
				select history_produk
				from t_history
				group by history_produk
				order by count(*) desc
			)
			limit 0,10";*/
		$rs = $this->db->query($sql);
	
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return NULL;
		}
	}
	function detail_produk($id){
		$sql = "SELECT * FROM m_produk WHERE produk_id = '" . $id . "' LIMIT 0,1";
		$rs = $this->db->query($sql);
		if($rs->num_rows()){
			return json_encode($rs->result());
		}else{
			return NULL;
		}
	}
	function incart_produk($id){
		if($id !== '' && $id !== false){
			$sql = "SELECT * FROM m_produk WHERE produk_id IN(" . $id . ")";
			$rs = $this->db->query($sql);
			if($rs->num_rows()){
				return $rs->result();
			}else{
				return NULL;
			}
		}else{
			return '';
		}
	}
	function get_saldouser($user_id){
		if($user_id !== '' && $user_id !== false){
			$sql = "select member_saldo from m_member where member_users = '" . $user_id . "' limit 0,1";
			$rs = $this->db->query($sql);
			if($rs->num_rows()){
				$result = $rs->row();
				return $result->member_saldo;
			}else{
				return NULL;
			}
		}else{
			return '';
		}
	}
	function reset_saldouser($user_id, $saldo){
		if($user_id !== '' && $user_id !== false){
			$sql = "update m_member set member_saldo = '" . $saldo . "' where member_users = '" . $user_id . "'";
			$rs = $this->db->query($sql);
			if($this->db->affected_rows()){
				return 1;
			}else{
				return 0;
			}
		}else{
			return 0;
		}
	}
	function generate_topupdeposit($jumlah, $user_id, $generate_kode, $tanggal_create, $tanggal_exp, $bank){
		$data = array(
			'tiket_kode' => $generate_kode + $jumlah,
			'tiket_genkode' => $generate_kode,
			'tiket_rupiah' => $jumlah,
			'tiket_users' => $user_id,
			'tiket_gentgl' => $tanggal_create,
			'tiket_tglexp' => $tanggal_exp,
			'tiket_bank' => $bank
		);
		$this->db->insert('t_tiket', $data);
		if($this->db->affected_rows()){
			$kuery = "select bank_rek from m_bank where bank_id = '". $bank ."'";
			$query = $this->db->query($kuery);
			$row = $query->row();
			$no_rek = $row->bank_rek;
			
			$data = array(
				'kode' => $generate_kode + $jumlah,
				'rekening' => $no_rek
			);
			foreach($data as $row){
				$arr[] = $row;
			}
			return json_encode($data);
		}else{
			return 0;
		}
	}
	function konfirmasi($kode, $user_id, $tanggal_update){
		$data = array(
			'tiket_status' => 'confirm'
		);
		$this->db->where('tiket_kode', $kode);
		$this->db->where('tiket_users', $user_id);
		$this->db->where('tiket_status', 'pending');
		$this->db->update('t_tiket', $data);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	function get_today_lastkode($now){
		//$sql = "select max(tiket_genkode) as last_kode from t_tiket where date_format(tiket_gentgl,'%d-%m-%Y') = date_format('" . $now . "','%d-%m-%Y')";
		$sql = "select tiket_genkode from t_tiket where date_format(tiket_gentgl,'%d-%m-%Y') = date_format('" . $now . "','%d-%m-%Y')";
		$rs = $this->db->query($sql);
		$num_rows = $rs->num_rows();
		if($num_rows > 0){
			return $rs->result();
		}else{
			return 0;
		}
	}
	function insert_history($produkid , $user_id, $now){
		$data = array(
			'history_produk' => $produkid,
			'history_users' => $user_id,
			'history_tglbeli' => $now,
		);
		$query = $this->db->insert('t_history', $data);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	function product_havebought_list($user_id, $sort, $start, $end){
		$kuery = "SELECT * FROM t_history 
		JOIN m_produk ON t_history.history_produk = m_produk.produk_id
		WHERE history_users = '" . $user_id . "'
		ORDER BY " . $sort . "
		LIMIT " . $start . "," . $end;
		$query = $this->db->query($kuery);
		$num_rows = $query->num_rows();
		if($num_rows > 0){
			$result = $query->result();
			return $result;
		}else{
			return 0;
		}
	}
	function count_producthavebought($user_id){
		$kuery = "SELECT COUNT(*) as produk_count FROM t_history
			WHERE history_users = '" . $user_id . "'
		";
		$query = $this->db->query($kuery);
		$row = $query->row();
		return $row->produk_count;
	}
	function get_produk_url($product_id){
		$kuery = "SELECT produk_file as url from m_produk where produk_id = '". $product_id ."' LIMIT 0,1";
		$query = $this->db->query($kuery);
		$num_rows = $query->num_rows();
		if($num_rows > 0){
			$row = $query->row();
			return $row->url;
		}
	}
	function get_banklist(){
		$kuery = "SELECT * FROM m_bank";
		$query = $this->db->query($kuery);
		$num_rows = $query->num_rows();
		if($num_rows > 0){
			$result = $query->result();
			return $result;
		}else{
			return '';
		}
	}
}

?>